blob: 5099f4fd0a6760405fc58360eba3deb7ef522dab (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
import dbConnect from 'configs/dbConnect'
import withSession from 'hocs/withSession'
import NoteList from 'apps/Notes/models/NoteList'
import Note from 'apps/Notes/models/Note'
export default withSession(async (req, res) => {
const conn = await dbConnect()
const { id: _id } = req.query
switch (req.method) {
case 'GET':
try {
const user = req.session.get('user')
if (!user || !user?.isVerified || !_id) {
throw new Error('Something went wrong')
}
const note = await Note.getNote(_id)
if (!note) {
throw new Error('Something went wrong')
}
res.status(200).json(note)
} catch (error) {
res.status(400).json({ error: true })
}
break
case 'DELETE':
try {
const session = await conn.startSession()
const user = req.session.get('user')
if (!user || !user?.isVerified || !_id) {
throw new Error('Something went wrong')
}
const noteId = await NoteList.getNoteId(user.noteList, _id)
if (!noteId) throw new Error('Something went wrong')
await session.withTransaction(async () => {
await Note.findByIdAndDelete(noteId)
const { notes } = await NoteList.removeNote(user.noteList, _id)
session.endSession()
res.status(200).json(notes)
})
} catch (error) {
res.status(400).json([])
}
break
case 'PUT':
try {
const session = await conn.startSession()
const user = req.session.get('user')
const { title, noteId, content } = JSON.parse(req.body)
if (!user || !user?.isVerified || !_id || !content) {
throw new Error('Something went wrong')
}
await session.withTransaction(async () => {
await Note.updateNote(noteId, content)
const { notes } = await NoteList.updateList(user.noteList, noteId, title)
session.endSession()
res.status(200).json(notes)
})
} catch (error) {
res.status(400).json([])
}
break
}
})
|